package Finding_Numbers_by_Dichotomy;
//普通的太简单,c语言中学过


//这里写一些变种
//找一个大于等于一个数的最左边的数//向右同理
public class test1 {
    public static void main(String[] args) {
        int num = 5;
        int[] arr = {1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9};
        int decade = mostLeftNoLessNumIndex(arr, num);
        if (decade != -1) {
            System.out.println(arr[decade] + "下标为:" + decade);
        } else {
            System.out.println("没有");
        }
    }

    public static int mostLeftNoLessNumIndex(int[] arr, int num) {
        if (arr == null || arr.length == 0)
            return -1;
        int L = 0;
        int R = arr.length - 1;
        int ans = -1;
        while (L <= R) {
            int mid = (L + R) / 2;
            if (arr[mid] >= num) {
                ans = mid;
                R = mid - 1;
            } else {
                L = mid + 1;
            }
        }
        return ans;
    }
}
